package com.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.biz.TCompetenceBIZ;
import com.biz.TMenutreeBIZ;
import com.entity.TCompetence;
import com.entity.TMenutree;
import com.util.CookieUtil;
import com.util.ReturnValue;

@Controller
@SuppressWarnings("rawtypes")
public class TCompetenceController {
	private TCompetenceBIZ tCompetenceBIZ;
	@Resource
	
	public void settCompetenceBIZ(TCompetenceBIZ tCompetenceBIZ) {
		this.tCompetenceBIZ = tCompetenceBIZ;
	}
	
	private TMenutreeBIZ tMenutreeBIZ;
	
	@Resource
	public void settMenutreeBIZ(TMenutreeBIZ tMenutreeBIZ) {
		this.tMenutreeBIZ = tMenutreeBIZ;
	}

	
	
	/**
	 * 异步获取菜单树 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/doAsyncGetCompetenceInit.do")
	public void doAsyncGetCompetenceInit(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		Cookie cookie = CookieUtil.getCookieByName(request, "user_type_id");
		String id= request.getParameter("id");
		if(cookie==null){
			response.sendRedirect(basePath+"login/login.jsp");
		}
		String utypeId = cookie.getValue();
		TCompetence instance = new TCompetence();
		instance.setUtypeId(utypeId);
		instance.setParentId(id==null?"0":id);
		List tmp = this.tCompetenceBIZ.findByExample(instance);
		List<Map<String, String>> reList = new ArrayList<Map<String, String>>();
		for (int i = 0; i < tmp.size(); i++) {
			TCompetence tCompetence =(TCompetence) tmp.get(i);
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", tCompetence.getMenutreeId());
			map.put("pId",tCompetence.getParentId());
			TMenutree menuTreeObj = this.tMenutreeBIZ.findById(tCompetence.getMenutreeId());
			map.put("name", menuTreeObj.getName());
			map.put("url",menuTreeObj.getUrl()==null?"":menuTreeObj.getUrl());
			List menuTrees = this.tMenutreeBIZ.findByParentId(tCompetence.getMenutreeId());
			if(menuTrees.size()>0){
				map.put("isParent", "true");
			}
			reList.add(map);
		}
		
		try {
			ReturnValue.getResponse(response, reList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
